package com.gemom.gui;

import javax.swing.*;
import javax.swing.border.LineBorder;

import java.awt.*;

import java.text.NumberFormat;

//javax.swing.GroupLayout
/**
 *
 * @author  Yair Diaz
 */
public class CovMatrixGUI extends JPanel{

    private int numberitems;
    
    private JLabel [][]jLabels;
    private JPanel panel;
    private JScrollPane jscroll;
    
    // End of variables declaration
    
    public CovMatrixGUI(int numberitems, int xsize,int ysize) {
        
        GridLayout gl=new GridLayout(numberitems,numberitems);
        gl.setHgap(5);
        gl.setVgap(3);
        panel = new JPanel(gl);
        panel.setBackground(Color.white);
        panel.setBorder(BorderFactory.createTitledBorder("VAR COV Matrix"));
        jLabels = new JLabel[numberitems][numberitems];
        
        this.numberitems=numberitems;
         for(int i=0;i<numberitems;i++)
            for(int k=0;k<numberitems;k++)
                {   
                    JLabel lab =new JLabel();
                    lab.setOpaque(true);
                    lab.setBorder(new LineBorder(Color.blue));
                    lab.setPreferredSize(new Dimension(80,15));
                    lab.setFont(new Font("Dialog", Font.PLAIN, 11));
                    lab.setHorizontalAlignment(SwingConstants.CENTER); 
                    String st= " ("+i+","+k+")";      
                    if(i!=k){lab.setText("Cov"+st);}
                    if(i==k){lab.setText("Var"+st);lab.setBackground(new Color(210,210,210));}
                    jLabels[i][k] =lab;
                }
              
        for(int i=0;i<numberitems;i++)
            for(int k=0;k<numberitems;k++)
                 panel.add(jLabels[i][k]);
        
        jscroll= new JScrollPane(panel);
        jscroll.setPreferredSize(new Dimension(xsize,ysize));
  
        this.add(jscroll);

        }
    
    //refreshing the matrix    
    public void resetMatrixlabels()
        {   
            for(int i=0;i<numberitems;i++)
                for(int k=0;k<numberitems;k++)
                    {String st= " ("+i+","+k+")";
                     if(i!=k){jLabels[i][k].setText("Cov"+st);}
                     if(i==k){jLabels[i][k].setText("Var"+st);}
                    }
        }
    
    public void enterCovMatrixData(double [][]covMatrix)
        {
            NumberFormat nf = NumberFormat.getInstance();
            nf.setMaximumFractionDigits(2);
        
            for(int i=0;i<numberitems;i++)
                 for(int k=0;k<numberitems;k++)
                 {
                     String st= " ("+i+","+k+")";  
                     jLabels[i][k].setText(nf.format(covMatrix[i][k])+st);
                 }
    
        }
    
}
